package com.base.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.base.entity.bo.ExpertBo;
import com.base.entity.bo.ExpertGroupBO;
import com.base.entity.bo.ExpertGroupRelateBo;
import com.base.entity.bo.SciencePersonBo;
import com.base.entity.vo.ExpertGroupVo;
import com.base.entity.vo.ExpertVo;
import com.base.entity.vo.SciencePersonVo;
import com.base.mapper.ExpertGroupMapper;
import com.base.mapper.ExpertGroupRelateMapper;
import com.base.mapper.SciencePersonMapper;
import com.base.utils.CutPageBean;
//import com.common.feign.BusinessStatusFeign;
import com.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExpertGroupService {
    @Autowired
    ExpertGroupMapper expertGroupMapper;

    @Autowired
    ExpertGroupRelateMapper expertGroupRelateMapper;

    public CutPageBean<ExpertGroupBO> selectExpertGroupList(ExpertGroupVo expertGroupVo) {
        expertGroupVo.setStart(expertGroupVo.getPage(),expertGroupVo.getPageSize());
        CutPageBean<ExpertGroupBO> cutPage = new CutPageBean<>();
        List<ExpertGroupBO> list = expertGroupMapper.selectExpertGroupList(expertGroupVo);
        List<String> id=list.stream().map(ExpertGroupBO::getId).collect(Collectors.toList());
        ExpertGroupRelateBo expertGroupRelateBo=new ExpertGroupRelateBo();
        for(int i = 0; i < list.size(); i++) {
            expertGroupRelateBo.setGroupId(id.get(i));
            List<ExpertGroupRelateBo> list1 = expertGroupRelateMapper.ExpertGroupRelateCount(expertGroupRelateBo);
            List<String> count=list1.stream().map(ExpertGroupRelateBo::getCount).collect(Collectors.toList());
            list.get(i).setCount(count.get(0).toString());
        }
        int count = expertGroupMapper.selectExpertGroupCount(expertGroupVo);
        cutPage.initCutPage(count,expertGroupVo.getPageSize(),list);
        return cutPage;
    }

    public  int insertExpertGroup(ExpertGroupBO expertGroupBO) {
        expertGroupBO.setId(IdUtils.simpleUUID());
        int row=expertGroupMapper.insert(expertGroupBO);
        return row;
    }

    public int updateExpertGroup(ExpertGroupBO expertGroupBO) {
        int row=expertGroupMapper.update(expertGroupBO,new UpdateWrapper<ExpertGroupBO>().eq("ID",expertGroupBO.getId()));
        return row;
    }

    public  int deletExpertGroup(ExpertGroupBO expertGroupBO) {
        String[] result = expertGroupBO.getIdList();
        for (int i = 0; i < result.length; i++) {
            Map<String,Object> map=new HashMap<>();
            map.put("GROUP_ID",result[i]);
            expertGroupRelateMapper.deleteByMap(map);
        }
        int row =0;
        for (int i = 0; i < result.length; i++) {
            Map<String,Object> map=new HashMap<>();
            map.put("id",result[i]);
            row=expertGroupMapper.deleteByMap(map);
        }
        return row;
    }

    public int insertExpertGroupPerson(ExpertGroupRelateBo expertGroupRelateBo) {
        if(expertGroupRelateBo.getExpertIdList() !=null && expertGroupRelateBo.getExpertIdList().length > 0){
            Map<String,Object> map1=new HashMap<>();
            map1.put("GROUP_ID",expertGroupRelateBo.getGroupId());
            expertGroupRelateMapper.deleteByMap(map1);
            String[] result = expertGroupRelateBo.getExpertIdList();
            int row =0;
            for (int i = 0; i < result.length; i++) {
                expertGroupRelateBo.setId(IdUtils.simpleUUID());
                expertGroupRelateBo.setExpertId(result[i]);
                row=expertGroupRelateMapper.insert(expertGroupRelateBo);
            }
                return row;
            }else {
            Map<String,Object> map1=new HashMap<>();
            map1.put("GROUP_ID",expertGroupRelateBo.getGroupId());
            int row=expertGroupRelateMapper.deleteByMap(map1);
            row=2;
            return row;
            }
        }

    public  int deletExpertGroupPerson(ExpertGroupRelateBo expertGroupRelateBo) {
        if(expertGroupRelateBo.getExpertIdList() !=null || expertGroupRelateBo.getExpertIdList().length > 0){
            String[] result = expertGroupRelateBo.getGroupIdList();
            int row =0;
            for (int i = 0; i < result.length; i++) {
                Map<String,Object> map=new HashMap<>();
                map.put("GROUP_ID",result[i]);
                row=expertGroupRelateMapper.deleteByMap(map);
            }
            return row;
        }else {
            Map<String,Object> map=new HashMap<>();
            map.put("GROUP_ID",expertGroupRelateBo.getGroupId());
            int row=expertGroupRelateMapper.deleteByMap(map);
            return row;
        }
    }
}
